from fastapi import FastAPI

from config import settings
from routers import user_router

# class HeroBase(SQLModel):
#     hname: str
#     age: int | None = Field(default=None, index=True)
#
#
# class Hero(HeroBase, table=True):
#     id: int | None = Field(default=None, primary_key=True)
#     secret_name: str
#
#
# class HeroPublic(HeroBase):
#     id: int
#
#
# class HeroCreate(HeroBase):
#     secret_name: str
#
#
# class HeroUpdate(SQLModel):
#     hname: str | None = None
#     age: int | None = None
#     secret_name: str | None = None
#
#
# # 连接到数据库
# engine = create_engine(settings.database_url, connect_args=settings.connect_args)
#
#
# def create_db_and_tables():
#     SQLModel.metadata.create_all(engine)
#
#
# def get_session():
#     with Session(engine) as session:
#         yield session
#
#
# SessionDep = Annotated[Session, Depends(get_session)]

app = FastAPI()

app.include_router(user_router, prefix="/users", tags=["users"])


# @app.on_event("startup")
# def on_startup():
#     create_db_and_tables()


@app.get("/")
def read_root():
    return {"message": "Hello World"}


@app.get("/info")
async def info():
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
        "debug": settings.debug,
        "port": settings.port,
        "database_url": settings.database_url,
    }


# @app.post("/heroes/", response_model=HeroPublic)
# def create_hero(hero: HeroCreate, session: SessionDep):
#     db_hero = Hero.model_validate(hero)
#     session.add(db_hero)
#     session.commit()
#     session.refresh(db_hero)
#     return db_hero
#
#
# @app.get("/heroes/", response_model=list[HeroPublic])
# def read_heroes(
#     session: SessionDep,
#     offset: int = 0,
#     limit: Annotated[int, Query(le=100)] = 100,
# ):
#     heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
#     return heroes
#
#
# @app.get("/heroes/{hero_id}", response_model=HeroPublic)
# def read_hero(hero_id: int, session: SessionDep):
#     hero = session.get(Hero, hero_id)
#     if not hero:
#         raise HTTPException(status_code=404, detail="Hero not found")
#     return hero
#
#
# @app.patch("/heroes/{hero_id}", response_model=HeroPublic)
# def update_hero(hero_id: int, hero: HeroUpdate, session: SessionDep):
#     hero_db = session.get(Hero, hero_id)
#     if not hero_db:
#         raise HTTPException(status_code=404, detail="Hero not found")
#     hero_data = hero.model_dump(exclude_unset=True)
#     hero_db.sqlmodel_update(hero_data)
#     session.add(hero_db)
#     session.commit()
#     session.refresh(hero_db)
#     return hero_db
#
#
# @app.delete("/heroes/{hero_id}")
# def delete_hero(hero_id: int, session: SessionDep):
#     hero = session.get(Hero, hero_id)
#     if not hero:
#         raise HTTPException(status_code=404, detail="Hero not found")
#     session.delete(hero)
#     session.commit()
#     return {"ok": True}
#
#
# if __name__ == "__main__":
#     uvicorn.run(app, host="0.0.0.0", port=8000)
